<template>
  <el-dialog :title="title" :visible.sync="visible" width="1000px" append-to-body :show-close="false" :close-on-click-modal="false"	>
    <div class="images-condition-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="图片类型" prop="imageType">
          <el-select v-model="queryParams.imageType" placeholder="请选择图片类型" clearable>
            <el-option v-for="dict in dict.type.sys_goods_img_type" :key="dict.value" :label="dict.label"
                       :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="存储源" prop="imageServer">
          <el-select v-model="queryParams.imageServer" placeholder="请选择图片存储源" clearable>
            <el-option v-for="dict in dict.type.sys_goods_res_path" :key="dict.value" :label="dict.label"
                       :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="商品编码" prop="gNumber">
          <el-input v-model="queryParams.gNumber" placeholder="请输入商品编码" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="商品名称" prop="gName">
          <el-input v-model="queryParams.gName" placeholder="请输入商品名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="图片名称" prop="imageName">
          <el-input v-model="queryParams.imageName" placeholder="请输入图片名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="状态" prop="delFlag">
          <el-select v-model="queryParams.delFlag" placeholder="图片状态" clearable>
            <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label"
                       :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="images-list-container">
      <el-table v-loading="loading" border fit stripe :data="list" max-height="400px" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" :selectable="isSelectable" />
        <!-- <el-table-column label="图片id" align="center" prop="imageId" /> -->
        <el-table-column label="商品编码" align="center" prop="gNumber" width="220" :show-overflow-tooltip="true" />
        <el-table-column label="商品名称" align="center" prop="gName" :show-overflow-tooltip="true" />
        <el-table-column label="图片类型" align="center" prop="imageType" width="80">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_goods_img_type" :value="scope.row.imageType" />
          </template>
        </el-table-column>
        <el-table-column label="图片名称" align="center" prop="imageName" :show-overflow-tooltip="true" />
        <el-table-column label="图片" align="center" prop="imagePath" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.imagePath" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSelectImage">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { findNoSelImagesList } from '@/api/goods/images'

export default {
  dicts: ['sys_goods_img_type', 'sys_goods_res_path', 'sys_normal_disable'],
  props: {
    params: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      visible: false,
      title: '选择图片',
      limit: 1,
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 本地图片资源地址
      imgServe: process.env.VUE_APP_BASE_API,
      queryParams: {},
      list: []
    }
  },
  methods: {
    generateQueryParams() {

      return {
        pageNum: 1,
        pageSize: 10,
        gNumber: null,
        gName: null,
        imageType: null,
        imageName: null,
        imageServer: null,
        imagePath: null,
        orderNum: null,
        params: {
          gNumber: null,
        }
      }
    },
    // 显示弹框
    show() {
      this.queryParams = this.generateQueryParams();
      const params = this.params;
      if (params.gNumber) {
        this.queryParams.gNumber = params.gNumber;
        this.queryParams.params.gNumber = params.gNumber;
      }
      if (params.imageType) {
        this.queryParams.imageType = params.imageType;
      }
      if (params.limit) {
        this.limit = params.limit;
      } else {
        this.limit = 1;
      }
      this.getList();
      this.visible = true;
    },
    getList() {
      this.loading = true;
      findNoSelImagesList(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.imageId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleSelectImage() {
      const ids = this.ids;
      if (ids.length === 0) {
        this.$modal.msgError("请先选择图片");
      } else if (ids.length > this.limit) {
        this.$modal.msgError('最多选择 ' + this.limit + ' 张图片');
      } else {
        let imagePaths = [];
        const list = this.list;
        for (let i = 0, len = list.length; i < len; i++) {
          if (ids.includes(list[i].imageId) && list[i].imagePath) {
            imagePaths.push({ 'imageId': list[i].imageId, 'imagePath': list[i].imagePath });
          }
        }
        this.visible = false;// 提交表单时触发自定义事件
        this.$emit('ok', imagePaths);
      }
    },
    isSelectable(row, index) {
      if (row.params && row.params.imgId && row.params.imgId > 0) {
        return false;
      }
      return true;
    }
  }
}
</script>

<style scoped lang="scss">

</style>
